package com.preparation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.preparation.domain.User;
import com.preparation.dto.R;
import com.preparation.mapper.UserMapper;
import com.preparation.service.IUserService;
import com.preparation.untils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Date;
import java.util.Random;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Value("${images.path}")
    private String basePath;

    //注册
    @Override
    public R register(User user) {
        //查询用户是否存在
        if (findByUsername(user.getUsername()) != null){
            return R.error(409,"该用户已存在");
        }

        // 初始化参数
        user.setNickname("普通用户" + (1000 + new Random().nextInt(9000)));
        user.setGender(2);
        user.setFollowingCount(0);
        user.setFollowerCount(0);
        user.setCreateTime(new Date());
        user.setAccount_status("正常");

        //密码加密
        String md5String = Md5Util.getMD5String("lyt" + user.getPassword() + "community");
        user.setPassword(md5String);

        return userMapper.insert(user)>0 ? R.success("注册成功") : R.error();
    }

    //登录
    @Override
    public R login(User user) {
        // 判断用户是否存在
        User res = findByUsername(user.getUsername());
        if(res == null){
            return R.error(404, "用户名不存在！");
        }else{
            //加密对比
            String md5String = Md5Util.getMD5String("lyt" + user.getPassword() + "community");
            if (res.getPassword().equals(md5String)){
                return R.success(res, "登陆成功！");
            }else{
                return R.error(401, "密码错误！");
            }
        }
    }

    //修改
    @Override
    public boolean updateById(User user) {
        return super.updateById(user);
    }


    //修改用户头像
    @Override
    public Boolean updateAvatar(Integer id, String avatar) {
        String oldAvatar = userMapper.selectById(id).getAvatar();
        Boolean flag = userMapper.updateAvatar(id, avatar) > 0;
        if(flag){
            //上传成功删除用户原头像
            if (oldAvatar!= null &&!oldAvatar.isEmpty()) {
                deleteFile(oldAvatar);
            }
        }else{
            //上传失败，删除当前图片
            if (avatar!= null &&!avatar.isEmpty()) {
                // 构建完整的文件路径并删除文件
               deleteFile(avatar);
            }
        }

        return flag;
    }


    //根据用户名查询用户
    @Override
    public User findByUsername(String username) {
        //判断用户名是否存在
        User user = userMapper.getByUsername(username);
        return user;
    }

    // 文件删除
    private void deleteFile(String filePath) {
        int index = filePath.indexOf("name=");
        if (index!= -1) {
            filePath = filePath.substring(index + "name=".length());
        }
        File file = new File(basePath + filePath);
        if (file.exists()) {
            file.delete();
        }
    }


}
